package com.hisan.freeride.home.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.utils.ButtonUtils;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.utils.MyHandlerUtil;
import com.hisan.freeride.common.utils.utils.TokenUtils;
import com.hisan.freeride.common.view.SyLinearLayoutManager;
import com.hisan.freeride.databinding.WelfareMessageBinding;
import com.hisan.freeride.home.adapter.Welfare_Message_ListAdapter;
import com.hisan.freeride.home.model.WelfareMessage;
import com.hisan.freeride.home.model.WelfareModel;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.ufreedom.uikit.FloatingText;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间 : 2017/6/10
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：公益广场留言页面
 */
public class WelfareMessageActivity extends BaseActivity<WelfareMessageBinding> {
    private Welfare_Message_ListAdapter welfare_message_listAdapter;
    private int did;
    private WelfareModel welfareModel;
    private int page=1;
    private List<WelfareMessage> welfareMessages=new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.welfare_message;
    }

    @Override
    protected void initView() {
        mBinding.nineGrid.setSingleImageSize(200);
        mBinding.setStateModel(mStateModel);
        Bundle bundle=getIntent().getExtras();
        if(!CollectionUtils.isNullOrEmpty(bundle)){
            did=bundle.getInt("id");
            welfareModel= (WelfareModel) bundle.getSerializable("data");
            mBinding.setWelfareMessage(welfareModel);
            if (!CollectionUtils.isNullOrEmpty(welfareModel)){
                if (!CollectionUtils.isNullOrEmpty(welfareModel.getAvatar())) mBinding.logo.setImageURI(welfareModel.getAvatar());
                if (welfareModel.getIs_zan()==1){
                    mBinding.zan.setImageResource(R.mipmap.zan_fill);
                }else {
                    mBinding.zan.setImageResource(R.mipmap.zan);
                }
            }
            getReplylist();
        }
        mBinding.rv.setLayoutManager(new SyLinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        welfare_message_listAdapter=new Welfare_Message_ListAdapter(new ArrayList<>(0));
        mBinding.rv.setAdapter(welfare_message_listAdapter);
    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
        mBinding.frienddetailsOK.setOnClickListener(v -> {
            String token=TokenUtils.getToken();
            if (!CollectionUtils.isNullOrEmpty(token)){
                String content=mBinding.frienddetailsConnet.getText().toString();
                if (!CollectionUtils.isNullOrEmpty(content)){
                    mBinding.frienddetailsConnet.setText("");
                    ceatereprly(content);
                }
                else showToast("请输入你要发布的留言");
            }else {
                startActivityForResult(LoginActivity.class,0, null,true);
            }

        });
        mBinding.owner.setOnClickListener(v -> {
            if (!CollectionUtils.isNullOrEmpty(welfareModel)){
                Bundle bundle1 =new Bundle();
                bundle1.putInt("driver_id",welfareModel.getDriver_id());
                startActivityForResult(LoveCartDetailsActivity.class,0, bundle1,true);
            }
        });
        mBinding.pull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                page=1;
                welfareMessages.clear();
                getReplylist();
                mBinding.pull.finishRefresh();

            }

            @Override
            public void loadMore() {
                page++;
                getReplylist();
                mBinding.pull.finishLoadMore();
            }
        });
        mBinding.zan.setOnClickListener(v -> {
            String token= TokenUtils.getToken();
            if (!CollectionUtils.isNullOrEmpty(token)){
                if (welfareModel.getIs_zan()==1){

                    getzan(welfareModel.getId(),0);
                }else {
                    getzan(welfareModel.getId(),1);
                }
            }else {
                startActivityForResult(LoginActivity.class,0, null,true);
            }

        });
    }
    //点赞
    public void getzan(int id,int type) {
        OkGo.post(AppConfig.Getzan)
                .params("id",id)
                .execute(new DialogCallback<Object>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response)){
                            if (type==0){
                                mBinding.zan.setImageResource(R.mipmap.zan);
                                if (welfareModel.getZans()>=1) welfareModel.setZans(welfareModel.getZans()-1);
                                welfareModel.setIs_zan(0);
                                if (welfareModel.getIs_zan()==1){
                                    mBinding.zan.setImageResource(R.mipmap.zan_fill);
                                }else {
                                    mBinding.zan.setImageResource(R.mipmap.zan);
                                }
                                mBinding.setWelfareMessage(welfareModel);
                                welfareModel.setIs_zan(0);
                                showToast("取消成功");
                            }else {
                                if (!ButtonUtils.isFastDoubleClick()){
                                    FloatingText floatingText = new FloatingText.FloatingTextBuilder(WelfareMessageActivity.this)
                                            .textColor(Color.RED) // 漂浮字体的颜色
                                            .textSize(24)   // 浮字体的大小
                                            .textContent("+1") // 浮字体的内容
                                            .offsetY(50) // FloatingText 相对其所贴附View的垂直位移偏移量
                                            .build();
                                    floatingText.attach2Window(); //将FloatingText贴附在Window上
                                    //启动漂浮效果
                                    floatingText.startFloating(  mBinding.zan); // 传入一个View，FloatingText 就会相对于这个View执行漂浮效果
                                }
                                welfareModel.setZans(welfareModel.getZans()+1);
                                welfareModel.setIs_zan(1);
                                if (welfareModel.getIs_zan()==1){
                                    mBinding.zan.setImageResource(R.mipmap.zan_fill);
                                }else {
                                    mBinding.zan.setImageResource(R.mipmap.zan);
                                }
                                mBinding.setWelfareMessage(welfareModel);

                                showToast("点赞成功");
                            }

                            MyHandlerUtil.getHandler().postDelayed(() -> loadData(true),200);
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });

    }
    @Override
    protected void loadData(boolean isRefresh) {
        if (!CollectionUtils.isNullOrEmpty(welfareModel)){
            showData();
            if (welfareModel.getIs_zan()==1){
                mBinding.zan.setImageResource(R.mipmap.zan_fill);
            }else{
                mBinding.zan.setImageResource(R.mipmap.zan);
            }
            if (!CollectionUtils.isNullOrEmpty(welfareModel.getAlbum())) {
                ArrayList<ImageInfo> imageInfo = new ArrayList<>();
                for (String iamge : welfareModel.getAlbum()) {
                    ImageInfo info = new ImageInfo();
                    info.setThumbnailUrl(iamge);
                    info.setBigImageUrl(iamge);
                    imageInfo.add(info);
                }
                mBinding.nineGrid.setAdapter(new NineGridViewClickAdapter(WelfareMessageActivity.this, imageInfo));
            }
        }else {
            noData();
        }
    }

    //获取评论列表d
    private void getReplylist(){
        OkGo.<List<WelfareMessage>>get(AppConfig.Getreply)
                .tag(this)
                .params("id",did)
                .params("page",page)
                .params("size",10)
                .execute(new DialogCallback<List<WelfareMessage>>(this){
                    @Override
                    public void onSuccess(Response<List<WelfareMessage>> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())) {
                            welfareMessages.addAll(response.body());
                            if (welfareMessages.size()>10){
                                mBinding.pull.setCanLoadMore(true);
                            }else {
                                mBinding.pull.setCanLoadMore(false);
                            }
                            showData();
                            welfare_message_listAdapter.setNewData(welfareMessages);
                        }else {
                            if (!CollectionUtils.isNullOrEmpty(welfareMessages))
                                welfare_message_listAdapter.setNewData(welfareMessages);
                            else noData();
                        }
                    }

                    @Override
                    public void onError(Response<List<WelfareMessage>> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });

    }

    //发布评论
    private void ceatereprly(String content) {
        OkGo.post(AppConfig.Createreply)
                .tag(this)
                .params("id",did)
                .params("content",content)
                .execute(new DialogCallback<Object>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (isOk(response)){
                            showSuccessToast("评论成功");
                            int Comments= welfareModel.getComments()+1;
                            welfareModel.setComments(Comments);
                            mBinding.setWelfareMessage(welfareModel);
                            page=1;
                            welfareMessages.clear();
                            MyHandlerUtil.getHandler().postDelayed(() -> getReplylist(),200);
                        }else {
                            showError(response);
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });
    }
}
